#ifndef RESTCLIENT_H
#define RESTCLIENT_H

#include <QDir>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>

#include <qjson/parser.h>

#include "query.h"
#include "searchresult.h"

class RESTClient : public QObject
{
    Q_OBJECT
public:
    explicit RESTClient(QObject *parent = 0);
    bool isReady() const {return reply == NULL ? true : reply->isFinished();}
    QString search(const QString &query, const QStringList &stoplist);
    void clearIndex();
    void setSimilarity(const QString &similarity);
    void putDocument(QFile *doc, const QString &name, bool oneShot = true);
    void putAllDocuments(const QDir &directory);
    void getSearchResult(const Query &query, bool oneShot = true);
    void getAllSearchResults(const QList<Query> *queries);
    QList<SearchResult> getFoundDocs() {return searchResults;}

signals:
    void finished();

public slots:
    void finishedSlot(QNetworkReply *reply);

private:
    enum Mode{SEARCH, PUT_DOCUMENT, GET_SEARCH_RESULT};

    void putNextDocument();
    void getNextSearchResult();

    QNetworkAccessManager *qnam;
    QNetworkReply *reply;
    QJson::Parser parser;

    Mode mode;
    bool oneShot;

    QString searchResult;

    int currentFileIndex;
    QFile *currentFile;
    int fileNum;
    QString dirPath;
    QStringList files;

    const QList<Query> *queries;
    QList<SearchResult> searchResults;
};

#endif // RESTCLIENT_H
